package com.example.a_java.mapper;

import com.example.a_java.dto.ApprovalQuery;
import com.example.a_java.entity.ProductApproval;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 产品审核Mapper接口
 */
@Mapper
public interface ProductApprovalMapper {
    
    /**
     * 查询审核总数
     *
     * @param query 查询参数
     * @return 审核总数
     */
    @Select({
        "<script>",
        "SELECT COUNT(1) FROM t_product_approval",
        "<where>",
        "  <if test='name != null and name != \"\"'>",
        "    AND name LIKE CONCAT('%', #{name}, '%')",
        "  </if>",
        "  <if test='status != null and status != \"\"'>",
        "    AND status = #{status}",
        "  </if>",
        "</where>",
        "</script>"
    })
    Long countApprovals(ApprovalQuery query);

    /**
     * 查询审核列表
     *
     * @param query 查询参数
     * @return 审核列表
     */
    @Select({
        "<script>",
        "SELECT pa.* FROM t_product_approval pa",
        "<where>",
        "  <if test='name != null and name != \"\"'>",
        "    AND pa.name LIKE CONCAT('%', #{name}, '%')",
        "  </if>",
        "  <if test='status != null and status != \"\"'>",
        "    AND pa.status = #{status}",
        "  </if>",
        "</where>",
        "ORDER BY pa.submit_time DESC",
        "LIMIT #{pageSize} OFFSET #{offset}",
        "</script>"
    })
    @Results(id = "approvalMap", value = {
        @Result(property = "id", column = "id", id = true),
        @Result(property = "productId", column = "product_id"),
        @Result(property = "name", column = "name"),
        @Result(property = "type", column = "type"),
        @Result(property = "categoryId", column = "category_id"),
        @Result(property = "categoryName", column = "category_name"),
        @Result(property = "interestRate", column = "interest_rate"),
        @Result(property = "repaymentMethod", column = "repayment_method"),
        @Result(property = "applyCondition", column = "apply_condition"),
        @Result(property = "requiredMaterials", column = "required_materials"),
        @Result(property = "description", column = "description"),
        @Result(property = "submitter", column = "submitter"),
        @Result(property = "submitTime", column = "submit_time"),
        @Result(property = "status", column = "status"),
        @Result(property = "approver", column = "approver"),
        @Result(property = "approvalTime", column = "approval_time"),
        @Result(property = "approvalOpinion", column = "approval_opinion")
    })
    List<ProductApproval> selectApprovalList(ApprovalQuery query);

    /**
     * 查询审核详情
     *
     * @param id 审核ID
     * @return 审核详情
     */
    @Select({
        "SELECT * FROM t_product_approval WHERE id = #{id}"
    })
    @Results({
        @Result(property = "id", column = "id", id = true),
        @Result(property = "productId", column = "product_id"),
        @Result(property = "name", column = "name"),
        @Result(property = "type", column = "type"),
        @Result(property = "categoryId", column = "category_id"),
        @Result(property = "categoryName", column = "category_name"),
        @Result(property = "interestRate", column = "interest_rate"),
        @Result(property = "loanAmount", column = "loan_amount", typeHandler = com.example.a_java.handler.JsonTypeHandler.class),
        @Result(property = "loanTerm", column = "loan_term", typeHandler = com.example.a_java.handler.JsonTypeHandler.class),
        @Result(property = "repaymentMethod", column = "repayment_method"),
        @Result(property = "applyCondition", column = "apply_condition"),
        @Result(property = "requiredMaterials", column = "required_materials"),
        @Result(property = "description", column = "description"),
        @Result(property = "submitter", column = "submitter"),
        @Result(property = "submitTime", column = "submit_time"),
        @Result(property = "status", column = "status"),
        @Result(property = "approver", column = "approver"),
        @Result(property = "approvalTime", column = "approval_time"),
        @Result(property = "approvalOpinion", column = "approval_opinion"),
        @Result(property = "features", column = "id", 
                many = @Many(select = "com.example.a_java.mapper.ProductApprovalFeatureMapper.selectFeaturesByApprovalId", fetchType = FetchType.EAGER))
    })
    ProductApproval selectApprovalById(@Param("id") Long id);
    
    /**
     * 查询审核特性列�?
     * 
     * @param approvalId 审核ID
     * @return 审核特性列�?
     */
    @Select("SELECT * FROM t_product_approval_feature WHERE approval_id = #{approvalId}")
    @Results({
        @Result(property = "productId", column = "approval_id"),
        @Result(property = "key", column = "key"),
        @Result(property = "name", column = "name"),
        @Result(property = "value", column = "value")
    })
    List<com.example.a_java.entity.ProductFeature> selectApprovalFeatures(@Param("approvalId") Long approvalId);

    /**
     * 插入审核记录
     *
     * @param approval 审核信息
     * @return 影响行数
     */
    @Insert({
        "INSERT INTO t_product_approval (product_id, name, type, category_id, category_name,",
        "interest_rate, loan_amount, loan_term, repayment_method,",
        "apply_condition, required_materials, description,",
        "submitter, submit_time, status)",
        "VALUES (#{productId}, #{name}, #{type}, #{categoryId}, #{categoryName},",
        "#{interestRate},",
        "#{loanAmount, typeHandler=com.example.a_java.handler.JsonTypeHandler},",
        "#{loanTerm, typeHandler=com.example.a_java.handler.JsonTypeHandler},",
        "#{repaymentMethod}, #{applyCondition}, #{requiredMaterials},",
        "#{description}, #{submitter}, #{submitTime}, #{status})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertApproval(ProductApproval approval);

    /**
     * 更新审核记录
     *
     * @param approval 审核信息
     * @return 影响行数
     */
    @Update({
        "<script>",
        "UPDATE t_product_approval",
        "<set>",
        "  <if test='status != null'>status = #{status},</if>",
        "  <if test='approver != null'>approver = #{approver},</if>",
        "  <if test='approvalTime != null'>approval_time = #{approvalTime},</if>",
        "  <if test='approvalOpinion != null'>approval_opinion = #{approvalOpinion}</if>",
        "</set>",
        "WHERE id = #{id}",
        "</script>"
    })
    int updateApproval(ProductApproval approval);
} 
